package pl.edu.project.view;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import org.freixas.jcalendar.DateEvent;
import org.freixas.jcalendar.DateListener;
import org.freixas.jcalendar.JCalendar;
import org.freixas.jcalendar.JCalendarCombo;

@SuppressWarnings("serial")
public class FilterWindow extends JDialog {
    private class ActionCancel implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    dispose();
	}
    }
    private class ActionOK implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    dispose();
	    // dopisac
	}
    }
    private class MyDateListener implements DateListener {

	public void dateChanged(DateEvent e) {
	    selectedDate = e.getSelectedDate();
	}
    }
    private class MyTimeListener implements DateListener {

	public void dateChanged(DateEvent e) {
	    selectedTime = e.getSelectedDate();
	}
    }
    private JButton cancelButton;

    private JCalendarCombo dateChooser;
    private JLabel infoLabel;

    private CalendarFrame mainWindow;

    /**
	 * 
	 */
    private JButton okButton;

    private Calendar selectedDate;

    private Calendar selectedTime;

    private JCalendar timeChooser;

    public FilterWindow(JFrame owner) {
	super(owner, "Filtruj zdarzenia");
	setSize(350, 140);
	setLocation(owner.getLocation().x + 45, owner.getLocation().y + 115);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	// this.mainWindow = (CallendarFrame) owner;

	setData();
	selectedTime = timeChooser.getCalendar();
	selectedDate = dateChooser.getCalendar();
	// layout
	Container contentPane = getContentPane();
	contentPane.setLayout(new GridBagLayout());
	GridBagConstraints c = new GridBagConstraints();
	c.fill = GridBagConstraints.HORIZONTAL;
	c.insets.set(10, 5, 5, 5);
	c.gridwidth = 2;
	c.gridy = 0;
	c.gridx = 0;
	add(infoLabel, c);
	c.insets.set(1, 5, 1, 5);
	c.gridy = 1;
	add(dateChooser, c);
	c.gridwidth = 1;
	c.gridx = 2;
	add(timeChooser, c);
	c.gridwidth = 3;
	c.insets.set(10, 5, 5, 5);
	c.gridy = 2;
	c.gridx = 0;
	JPanel okCancel = new JPanel();
	okCancel.setAlignmentX(RIGHT_ALIGNMENT);
	okCancel.add(okButton);
	okCancel.add(cancelButton);
	add(okCancel, c);

	setResizable(false);
	setVisible(true);
    }

    private void setData() {
	this.okButton = new JButton("OK");
	okButton.addActionListener(new ActionOK());
	okButton.registerKeyboardAction(new ActionOK(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	this.cancelButton = new JButton("Anuluj");
	cancelButton.addActionListener(new ActionCancel());
	cancelButton.registerKeyboardAction(new ActionCancel(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	dateChooser = new JCalendarCombo(JCalendarCombo.DISPLAY_DATE, false);
	dateChooser.addDateListener(new MyDateListener());
	// listener
	this.timeChooser = new JCalendar(Calendar.getInstance(),
		Locale.getDefault(), JCalendar.DISPLAY_TIME, false, "HH:mm");
	timeChooser.addDateListener(new MyTimeListener());
	infoLabel = new JLabel("Usu� zdarzenia starsze, ni�...");
	infoLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
    }
}
